package com.lei.util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class DButils {

    private static Connection conn = null;
    private static String dbDriver = XMLParse.getConfigByTagName("db-driver");
    private static String dbAddress = XMLParse.getConfigByTagName("db-address");
    private static String dbName = XMLParse.getConfigByTagName("db-dbname");
    private static String dbUsername = XMLParse.getConfigByTagName("db-username");
    private static String dbPassword = XMLParse.getConfigByTagName("db-password");

    private DButils(){};


	/**关闭数据库连接
	 *
	 * @param conn 数据库连接串
	 * @param state
	 * @param rs
	 */
	public static void close(Connection conn,Statement state,ResultSet rs){

		try {
			if(rs != null)rs.close();
			if(state != null)state.close();
			if(conn != null && !conn.isClosed())conn.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}


	public static void main(String[] args) {

		Connection con = getConnection();
	}


	public static Connection getConnection(){
		String url = dbAddress;

		try {
			Class.forName(dbDriver);

		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}

		try {
			Connection conn = DriverManager.getConnection(url,dbUsername,dbPassword);
			return conn;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return conn;

	}

}
